import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    favorite: JSON.parse(window.localStorage.getItem("dg_favorite")) || [],
		lanzi: JSON.parse(window.localStorage.getItem("dg_lanzi")) || [],
  },
  mutations: {
    favoriteAdd: function(state, payload) {
			state.favorite = [payload.newCook, ...state.favorite];
			window.localStorage.setItem('dg_favorite', JSON.stringify(state.favorite))
		},
    favoriteReduce: function(state, payload) {			
      if(payload.id){
        var sy_index = state.favorite.findIndex( item => item.id == payload.id);
        // console.log(sy_index,22);
        state.favorite.splice(sy_index,1);
      }
      else{
        var sc_index = payload.index;
        // console.log(sc_index,11);
        state.favorite.splice(sc_index,1);
      }
			window.localStorage.setItem('dg_favorite', JSON.stringify(state.favorite))
		},
    lanziAdd: function(state, payload) {
      // state.lanzi:[{state.lanzi}]
      //payload.newM:{payload.newM}
			var tmp = [payload.newM, ...state.lanzi];
			state.lanzi = tmp;
			window.localStorage.setItem('dg_lanzi', JSON.stringify(state.lanzi))
		},
    lanziReduce: function(state, payload){
      // console.log(state, payload.index);
      state.lanzi.splice(payload.index,1);
      window.localStorage.setItem('dg_lanzi', JSON.stringify(state.lanzi))
    },
    lanziCaiDo: function(state, payload){
      state.lanzi.forEach((item,index) => {
        if(index == payload.index){
          item.major.forEach((i,o) => {
            if(o == payload.a){
              if(i.isDone){
                i["isDone"] = false;
              }
              else{
                i["isDone"] = true;
              }
            }
          });
        }
      });
      state.lanzi = [...state.lanzi];
      window.localStorage.setItem('dg_lanzi', JSON.stringify(state.lanzi))
    },
    lanziQingKong: function(state){
      state.lanzi = [];
      window.localStorage.setItem('dg_lanzi', JSON.stringify(state.lanzi));
    }
  },
  actions: {
  },
  modules: {
  }
})
